package cn.com.iswl.soap.client.controller;

import javax.xml.namespace.QName;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.iswl.soap.client.interceptor.ClientLoginInterceptor;
import cn.com.iswl.soap.service.User;

@RestController
@RequestMapping("user")
public class UserController {

    private static final String USER_NAME = "admin";
    private static final String PASS_WORD = "pass";

    @GetMapping("{userId}")
    public User getUsername(@PathVariable String userId) {
        try {
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient("http://localhost:8080/soap/user?wsdl");
            client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
            Object[] objects = new Object[0];
            objects = client.invoke(new QName("http://service.soap.iswl.com.cn/", "getUser"), userId);
            System.out.println(objects[0].getClass());
            System.out.println(objects[0].toString());
            User user = (User) objects[0];
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
